
/* 检查表单是否合法 */
function validateForm(username, password) {
    'use struct';
	if (username === "" || password === "") {
		window.alert("用户名和密码不能为空，请填写完整！");
		return false;
	} else if (password.length < 6) {
		window.alert("密码需要大于6位，请填写完整。")
		return false;
	}
	return true;
}

var form = document.querySelector('form');
/* 注册提交表单的回调函数 */
form.addEventListener('submit', function(event) {
    var username = document.getElementById('username').value;
    var password = document.getElementById('password').value;
	if (validateForm(username, password)) {

        event.preventDefault();
	    console.log(new Date())
	    
		let xhr = new XMLHttpRequest()
		xhr.open("POST", "/login?username="+username+"&password="+password, true)
		xhr.onreadystatechange = () => {
    		console.log(xhr)
		    if (xhr.readyState ==4 && xhr.status == 200) {
		        let res = JSON.parse(xhr.responseText)
    		    let id = parseInt(res)
    		    console.log(id)
		    }
		}
		xhr.send(null)

    }
});
